package controller

import (
	"context"
	v1 "wh-1-backend/api/v1"
	"wh-1-backend/internal/model"
	"wh-1-backend/internal/service"
)

type cUser struct{}

var User = cUser{}

func (u *cUser) List(ctx context.Context, req *v1.UserListReq) (res *v1.UserListRes, err error) {
	total, list, err := service.User().List(ctx, model.UserListInput{
		UserCreateInput: model.UserCreateInput{
			Nickname: req.Nickname,
			Account:  req.Account,
		},
		PageInput: model.PageInput{
			Page:     req.Page,
			PageSize: req.PageSize,
		},
	})
	if err != nil {
		return
	}
	res = &v1.UserListRes{
		Total: total,
		List:  list,
	}
	return
}

func (u *cUser) Create(ctx context.Context, req *v1.UserCreateReq) (res *v1.Res, err error) {
	err = service.User().Create(ctx, model.UserCreateInput{
		Nickname: req.Nickname,
		Account:  req.Account,
	}, req.Roles)
	return
}

func (u *cUser) Update(ctx context.Context, req *v1.UserUpdateReq) (res *v1.Res, err error) {
	err = service.User().Update(ctx, req.Id, model.UserCreateInput{
		Nickname: req.Nickname,
		Account:  req.Account,
	}, req.Roles)
	return
}

func (u *cUser) Delete(ctx context.Context, req *v1.UserDeleteReq) (res *v1.Res, err error) {
	err = service.User().Delete(ctx, req.Id)
	return
}

func (u *cUser) ResetPw(ctx context.Context, req *v1.UserResetPwReq) (res *v1.Res, err error) {
	err = service.User().ResetPw(ctx, req.Id)
	return
}

func (u *cUser) ChangePw(ctx context.Context, req *v1.UserChangePwReq) (res *v1.Res, err error) {
	err = service.User().ChangePw(ctx, model.UserChangePwInput{
		OldPassword: req.OldPassword,
		Password:    req.Password,
	})
	return
}
